﻿//**********************************************************
//Copyright(C)2011 By 波特商业立体网络公司版权所有。
//
//文件名：BackgroundWorkerExtension.cs
//文件功能：
//
//创建标识：米海波 || 2011-05-15
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Hmc.IM.Common
{
    /// <summary>
    /// BackgroundWorker扩展类
    /// </summary>
    public static class BackgroundWorkerExtension
    {
        #region 无结束消息通知

        /// <summary>
        /// 异步执行委托
        /// </summary>
        /// <param name="worker">扩展对象</param>
        /// <param name="obj">异步执行的委托</param>
        public static void AsynInvoke(this BackgroundWorker worker ,Action obj)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    obj();
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    (sender as BackgroundWorker).Dispose();
                }
            );
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 异步执行委托
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="obj">异步执行的委托</param>
        /// <param name="t">参数</param>
        public static void AsynInvoke<T>(this BackgroundWorker worker, Action<T> obj, T t)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    obj(t);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    (sender as BackgroundWorker).Dispose();
                }
            );
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="obj">异步执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        public static void AsynInvoke<T1, T2>(this BackgroundWorker worker, Action<T1, T2> obj, T1 t1, T2 t2)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    obj(t1, t2);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    (sender as BackgroundWorker).Dispose();
                }
            );
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <typeparam name="T3">参数类型3</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="obj">异步执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        public static void AsynInvoke<T1, T2, T3>(this BackgroundWorker worker, Action<T1, T2, T3> obj, T1 t1, T2 t2, T3 t3)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    obj(t1, t2, t3);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    (sender as BackgroundWorker).Dispose();
                }
            );
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <typeparam name="T3">参数类型3</typeparam>
        /// <typeparam name="T4">参数类型4</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="obj">异步执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        public static void AsynInvoke<T1, T2, T3, T4>(this BackgroundWorker worker, Action<T1, T2, T3, T4> obj, T1 t1, T2 t2, T3 t3, T4 t4)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    obj(t1, t2, t3, t4);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    (sender as BackgroundWorker).Dispose();
                }
            );
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <typeparam name="T3">参数类型3</typeparam>
        /// <typeparam name="T4">参数类型4</typeparam>
        /// <typeparam name="T5">参数类型5</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="obj">异步执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        public static void AsynInvoke<T1, T2, T3, T4, T5>(this BackgroundWorker worker, Action<T1, T2, T3, T4, T5> obj, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    obj(t1, t2, t3, t4, t5);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    (sender as BackgroundWorker).Dispose();
                }
            );
        }

        #endregion

        #region 有结束消息通知

        /// <summary>
        /// 异步调用
        /// </summary>
        /// <param name="worker">扩展对象</param>
        /// <param name="begin">异步执行的委托</param>
        /// <param name="end">异步完成后执行的委托</param>
        public static void AsynInvoke(this BackgroundWorker worker, Action begin, Action end)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    begin();
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    try
                    {
                        end();
                        (sender as BackgroundWorker).Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
            );
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 异步调用
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="begin">异步执行的委托</param>
        /// <param name="end">异步完成后执行的委托</param>
        /// <param name="t">参数1</param>
        public static void AsynInvoke<T>(this BackgroundWorker worker, Action<T> begin, Action end, T t)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    begin(t);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    try
                    {
                        end();
                        (sender as BackgroundWorker).Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
            );
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 异步调用
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="begin">异步执行的委托</param>
        /// <param name="end">异步完成后执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        public static void AsynInvoke<T1, T2>(this BackgroundWorker worker, Action<T1, T2> begin, Action end, T1 t1, T2 t2)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    begin(t1, t2);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    try
                    {
                        end();
                        (sender as BackgroundWorker).Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
            );
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <typeparam name="T3">参数类型3</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="begin">异步执行的委托</param>
        /// <param name="end">异步完成后执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        public static void AsynInvoke<T1, T2, T3>(this BackgroundWorker worker, Action<T1, T2, T3> begin, Action end, T1 t1, T2 t2, T3 t3)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    begin(t1, t2, t3);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    try
                    {
                        end();
                        (sender as BackgroundWorker).Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
            );
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <typeparam name="T3">参数类型3</typeparam>
        /// <typeparam name="T4">参数类型4</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="begin">异步执行的委托</param>
        /// <param name="end">异步完成后执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        public static void AsynInvoke<T1, T2, T3, T4>(this BackgroundWorker worker, Action<T1, T2, T3, T4> begin, Action end, T1 t1, T2 t2, T3 t3, T4 t4)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    begin(t1, t2, t3, t4);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    try
                    {
                        end();
                        (sender as BackgroundWorker).Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
            );
        }

        /// <summary>
        /// 异步调用
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T2">参数类型2</typeparam>
        /// <typeparam name="T3">参数类型3</typeparam>
        /// <typeparam name="T4">参数类型4</typeparam>
        /// <typeparam name="T5">参数类型5</typeparam>
        /// <param name="worker">扩展对象</param>
        /// <param name="begin">异步执行的委托</param>
        /// <param name="end">异步完成后执行的委托</param>
        /// <param name="t1">参数1</param>
        /// <param name="t2">参数2</param>
        /// <param name="t3">参数3</param>
        /// <param name="t4">参数4</param>
        /// <param name="t5">参数5</param>
        public static void AsynInvoke<T1, T2, T3, T4, T5>(this BackgroundWorker worker, Action<T1, T2, T3, T4, T5> begin, Action end, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
        {
            worker.DoWork += new DoWorkEventHandler(
                (sender, e) =>
                {
                    begin(t1, t2, t3, t4, t5);
                }
            );
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                (sender, e) =>
                {
                    try
                    {
                        end();
                        (sender as BackgroundWorker).Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
            );
        }

        #endregion
    }
}
